<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace User\Service;

use User\Data\CustomerDao;
use User\Data\CustomerAddressDao;
use Utilities\Session\SessionFacade;
use Zend\Log\Logger;

class CustomerService
{

    /**
     */
    protected $logger;
    
    /**
     */
    protected $customerDao;
    
    /**
     */
    protected $customerAddressDao;
    
    /**
     * 
     * @param CustomerDao $customerDao
     * @param Logger $logger
     */
    public function __construct(CustomerDao $customerDao, CustomerAddressDao $customerAddressDao, Logger $logger)
    {
        $this->customerAddressDao = $customerAddressDao;
    	$this->customerDao = $customerDao;
        $this->logger = $logger;
    }

    /**
     * 
     * @param string $firstName
     * @param string $lastName
     * @param string $email
     */
    public function updateCustomer($firstName, $lastName, $email) 
    {
        $sessionFacade = new SessionFacade();
        
        $customer = $this->customerDao->update($sessionFacade->getCustomer()->getId(), $firstName, $lastName, $email);
        
        $sessionFacade->setCustomer($customer);
    }
    
    /**
     * 
     * @param string $username
     * @param string $firstName
     * @param string $lastName
     * @param string $email 
     * @param string $provider
     */
    public function loginFromAuthenticationProvider($username, $firstName, $lastName, $email, $provider)
    {
        $this->logger->info("Logging in customer with username ".$username);
        
        // Check if a customer with given username exists
    	$customer = $this->customerDao->get($username);

    	// If customer has not been found, insert it
    	if ($customer == null) {
    	    $this->logger->info("Customer not found. Creating new customer with username ".$username);
    	    
    	    $customer = $this->customerDao->insert(uniqid(null, true), $username, $firstName, $lastName, $email, $provider);
    	}
    	    	
    	// Perform login saving customer in session
    	$sessionFacade = new SessionFacade();
    	
    	$sessionFacade->setCustomer($customer);
    }    
    
    /**
     * 
     */
    public function logout() 
    {
        // Perform logout deleting customer from session
        $sessionFacade = new SessionFacade();
         
        $sessionFacade->setCustomer(null);
    }

    /**
     * 
     */
    public function getAddresses()
    {
        $sessionFacade = new SessionFacade();

        if (is_null($sessionFacade->getCustomer()))
            return null;
        
        return $this->customerAddressDao->get($sessionFacade->getCustomer()->getId());
    }

    /**
     * 
     * @param string $street
     * @param string $number
     * @param string $city
     * @param string $zipCode
     * @param integer $stateId
     * @param string $state
     * @param integer $countryId
     * @param string $country
     */
    public function insertOrUpdateAddresses($street, $number, $city, $zipCode, $stateId, $state, $countryId, $country) 
    {
    	// Insert or update shipping address
        $this->insertOrUpdateAddress('shipping', $street, $number, $city, $zipCode, $stateId, $state, $countryId, $country);

    	// Insert or update billing address
        $this->insertOrUpdateAddress('billing', $street, $number, $city, $zipCode, $stateId, $state, $countryId, $country);
    }
    
    /**
     * 
     * @param string $type
     * @param string $street
     * @param string $number
     * @param string $city
     * @param string $zipCode
     * @param integer $stateId
     * @param string $state
     * @param integer $countryId
     * @param string $country
     */
    private function insertOrUpdateAddress($type, $street, $number, $city, $zipCode, $stateId, $state, $countryId, $country)
    {
        $sessionFacade = new SessionFacade();
        
        $address = $this->customerAddressDao->getByType($sessionFacade->getCustomer()->getId(), $type);
         
        if (is_null($address))
        	$this->customerAddressDao->insert($sessionFacade->getCustomer()->getId(), $type, $street, $number, $city, $zipCode, $state, $country, $stateId, $countryId);
        else
        	$this->customerAddressDao->update($address->getId(), $street, $number, $city, $zipCode, $state, $country, $stateId, $countryId);
    }
    
}

?>